package org.aphelie.torg.ruler.internal.core;

import org.eclipse.core.resources.IResource;


public abstract class AbstractNLModel extends AbstractModel {

    private static final long serialVersionUID = 1L;
    protected transient NLResourceHelper fNLHelper;

    public NLResourceHelper getNLResourceHelper() {
        if (fNLHelper == null)
            fNLHelper = createNLResourceHelper();
        return fNLHelper;
    }

    public void resetNLResourceHelper() {
        fNLHelper = null;
    }

    public void dispose() {
        if (fNLHelper != null) {
            fNLHelper.dispose();
            fNLHelper = null;
        }
        super.dispose();
    }

    public String getResourceString(String key) {
        if (key == null)
            return ""; //$NON-NLS-1$

        if (fNLHelper == null)
            fNLHelper = createNLResourceHelper();

        return fNLHelper != null ? fNLHelper.getResourceString(key) : key;
    }

    protected abstract NLResourceHelper createNLResourceHelper();

    public Object getAdapter(Class adapter) {
        if (adapter == IResource.class) {
            IResource resource = getUnderlyingResource();
            return resource == null ? null : resource.getProject();
        }
        return super.getAdapter(adapter);
    }

}
